package org.wtframework.website.web.mvc.action.impl;
import java.text.ParseException;
import java.util.List;
import java.util.Map;
import org.openredhead.springx.core.server.page.IPage;
import org.openredhead.springx.core.web.mvc.support.ActionValueAssistant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.wtframework.website.server.manager.FriendLinkManager;
import org.wtframework.website.server.service.FriendLinkService;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

@Controller
@RequestMapping("/back/website")
public class FriendLinkActionController {
	
	private static String BASE_URL = "admin/back_layui/website/";
	
	@Autowired
	private FriendLinkManager friendLinkManager;
	
	@Autowired
	private FriendLinkService friendLinkService;
	
	
	@RequestMapping("/friendLinkManager")
	public String friendLinkManager(ModelMap modelMap, ActionValueAssistant actionValueAssistant){		
		return BASE_URL+"friendLink/friendLinkManager";
	}
	
	@RequestMapping("/friendLinkData")
	@ResponseBody
	public Map<String,Object> friendLinkManagerUI(ModelMap modelMap, ActionValueAssistant actionValueAssistant)throws ParseException{
		Map<String,Object> resultMap = Maps.newHashMap();
		Integer pageNo = actionValueAssistant.get(Integer.class,"page");
		Integer pageSize = actionValueAssistant.get(Integer.class, "limit");//layui默认pageSize
		String title =actionValueAssistant.get(String.class,"title");
		if(pageNo==null){
			pageNo=1;
		}
		List<Object> valueList = Lists.newArrayList();
		String hql="select id ,orderId,title,linkUrl from FriendLink ";
		if(!Strings.isNullOrEmpty(title)){
			hql+="where title like ? ";
			valueList.add("%"+title+"%");
		}
		IPage<Map<String,Object>> page = friendLinkManager.findProperty(new String[]{"id","orderId","title","linkUrl",},hql,valueList.toArray()).getPageByPageNo(pageNo, pageSize);
		Integer beginIndex = (pageNo-1)*pageSize;
		for(Map<String,Object> map:page.getData()){
			map.put("index", ++beginIndex);
		}
		resultMap.put("count", page.getTotalCount());
		resultMap.put("data", page.getData());
		resultMap.put("code", 0);
		resultMap.put("msg", "");//当数据出错是输出该句子
		
		return resultMap;
	}
	
	/**
	 * 
	 * <B>方法描述：</B>（友情链接的添加修改方法） <br/>
	 *
	 * <B>作者：	 </B> 何展操 <br/>
	 * <B>创建日期：</B> 2014年7月16日 上午11:36:23 
	 * @param modelMap
	 * @param actionValueAssistant
	 * @return
	 */
	@RequestMapping("/friendLinkUpdate")
	public String friendLinkAddOrUpdate(ModelMap modelMap, ActionValueAssistant actionValueAssistant){
		Integer friendId = actionValueAssistant.get(Integer.class, "friendId");
		if(friendId!=null){
			modelMap.put("friendLink", friendLinkService.findFriendById(friendId));
		}
		return BASE_URL+"friendLink/friendLinkUpdate";
	}
	
}
